package com.processmanager.controller;

import com.processmanager.sdk.model.ProcessInfo;
import com.processmanager.service.ProcessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 进程管理REST API控制器
 * 
 * <p>提供进程管理的HTTP API接口：</p>
 * <ul>
 *   <li>执行命令</li>
 *   <li>获取进程信息</li>
 *   <li>停止进程</li>
 *   <li>获取进程列表</li>
 * </ul>
 * 
 * @author ProcessManager
 * @version 1.0.0
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/api/processes")
@Tag(name = "进程管理", description = "进程管理相关API")
public class ProcessController {
    
    private final ProcessService processService;
    
    /**
     * 构造函数
     * 
     * @param processService 进程管理服务
     */
    public ProcessController(ProcessService processService) {
        this.processService = processService;
    }
    
    /**
     * 执行命令
     * 
     * @param request 命令执行请求
     * @return 进程ID
     */
    @PostMapping("/execute")
    @Operation(summary = "执行命令", description = "异步执行指定的命令行命令")
    public ResponseEntity<ExecuteCommandResponse> executeCommand(
            @RequestBody ExecuteCommandRequest request) {
        
        try {
            String processId = processService.executeCommand(request.getCommand(), request.getName());
            return ResponseEntity.ok(new ExecuteCommandResponse(processId, "命令执行成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(new ExecuteCommandResponse(null, "命令执行失败: " + e.getMessage()));
        }
    }
    
    /**
     * 获取所有进程信息
     * 
     * @return 进程信息列表
     */
    @GetMapping
    @Operation(summary = "获取进程列表", description = "获取所有进程的详细信息")
    public ResponseEntity<Map<String, ProcessInfo>> getAllProcesses() {
        Map<String, ProcessInfo> processes = processService.getAllProcesses();
        return ResponseEntity.ok(processes);
    }
    
    /**
     * 获取指定进程信息
     * 
     * @param processId 进程ID
     * @return 进程信息
     */
    @GetMapping("/{processId}")
    @Operation(summary = "获取进程信息", description = "根据进程ID获取指定进程的详细信息")
    public ResponseEntity<ProcessInfo> getProcessInfo(
            @Parameter(description = "进程ID") @PathVariable String processId) {
        
        ProcessInfo processInfo = processService.getProcessInfo(processId);
        if (processInfo == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(processInfo);
    }
    
    /**
     * 停止进程
     * 
     * @param processId 进程ID
     * @return 操作结果
     */
    @PostMapping("/{processId}/stop")
    @Operation(summary = "停止进程", description = "停止指定的进程")
    public ResponseEntity<StopProcessResponse> stopProcess(
            @Parameter(description = "进程ID") @PathVariable String processId) {
        
        boolean success = processService.stopProcess(processId);
        if (success) {
            return ResponseEntity.ok(new StopProcessResponse("进程停止成功"));
        } else {
            return ResponseEntity.badRequest()
                    .body(new StopProcessResponse("进程停止失败"));
        }
    }
    
    /**
     * 命令执行请求
     */
    public static class ExecuteCommandRequest {
        private String command;
        private String name;
        
        public ExecuteCommandRequest() {}
        
        public ExecuteCommandRequest(String command, String name) {
            this.command = command;
            this.name = name;
        }
        
        public String getCommand() {
            return command;
        }
        
        public void setCommand(String command) {
            this.command = command;
        }
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
    }
    
    /**
     * 命令执行响应
     */
    public static class ExecuteCommandResponse {
        private String processId;
        private String message;
        
        public ExecuteCommandResponse() {}
        
        public ExecuteCommandResponse(String processId, String message) {
            this.processId = processId;
            this.message = message;
        }
        
        public String getProcessId() {
            return processId;
        }
        
        public void setProcessId(String processId) {
            this.processId = processId;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    /**
     * 停止进程响应
     */
    public static class StopProcessResponse {
        private String message;
        
        public StopProcessResponse() {}
        
        public StopProcessResponse(String message) {
            this.message = message;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
} 